﻿using SchoolManagementSystem.Classes;
using SchoolManagementSystem.Exceptions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SchoolManagementSystem.Forms
{
    public partial class ParentForm : Form
    {
        private SQLiteConnecter sqliteConnecter;
        private Logger logger=new Logger();
        private SelectForm selectForm;
        private InsertForm insertForm;

        public ParentForm()
        {
            InitializeComponent();
            this.FormClosed += exit;

            string sqlite = "schoolManagement.sqlite";
            sqliteConnecter = new SQLiteConnecter(logger);
            try
            {
                sqliteConnecter.createSqlite(sqlite);
            }
            catch (ConnectFailedException exp)
            {
                System.Console.WriteLine(exp.ToString());
            }
            try
            {
                sqliteConnecter.connectSqlite(sqlite);
            }
            catch (ConnectFailedException exp)
            {
                System.Console.WriteLine(exp.ToString());
            }

            selectForm = new SelectForm(this,sqliteConnecter);
            selectForm.Show();
            insertForm = new InsertForm(this,sqliteConnecter);
        }

        /// <summary>
        /// 点击工具栏退出选项，则关闭窗口退出程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemClose_Click(object sender, EventArgs e)
        {
            logger.close();
            sqliteConnecter?.close();
            this.Close();
            Environment.Exit(0);
        }

        /// <summary>
        /// 点击工具栏添加数据选项，子窗体跳转到添加子界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemInsert_Click(object sender, EventArgs e)
        {
            menuItemInsert.Text = "添加数据 √";
            menuItemSelect.Text = "查询数据";
            selectForm.Hide();
            insertForm.Show();
        }

        /// <summary>
        /// 点击工具栏查询数据选项，子窗体跳转到查询子界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemSelect_Click(object sender, EventArgs e)
        {
            menuItemInsert.Text = "添加数据";
            menuItemSelect.Text = "查询数据 √";
            selectForm.Show();
            insertForm.Hide();
        }

        /// <summary>
        /// 点击工具栏显示log记录的选项，执行以下方法，在nodepad打开相关文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemShowLog_Click(object sender, EventArgs e)
        {
            try
            {
                logger.showLogInfomation();
            }
            catch (Exception) { }
        }

        private void exit(Object sender, EventArgs args)
        {
            try
            {
                logger.close();
                sqliteConnecter?.close();
            }catch (Exception) { }
            Environment.Exit(0);
        }
    }
}
